package eteam.aps.controller;

import cn.hutool.core.bean.BeanUtil;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Assert;
import eteam.aps.common.helper.Helper;
import eteam.aps.model.dto.ProductNumberDto;
import eteam.aps.model.dto.ProductNumberSaveDto;
import eteam.aps.model.entity.ProductNumber;
import eteam.aps.model.vo.product_number.ProductNumberVo;
import eteam.aps.service.ProductNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品序号明细;产品序号控制层
 *
 * @author LiuLei
 */
@Api(tags = "产品序号明细")
@RestController
@RequestMapping("/pnc")
public class ProductNumberController {

    @Autowired
    private ProductNumberService productNumberService;

    @PostMapping("/spn")
    @ApiOperation("添加产品序号信息")
    @ApiImplicitParam(name = "List<ProductNumberVo>", value = "产品序号保存")
    public R<List<ProductNumberVo>> savePn(@RequestBody ProductNumberSaveDto productNumberSaveDto) throws Exception {
        Assert.isBlank(productNumberSaveDto.getfFront(), "前缀不能为空！");
        Assert.isBlank(productNumberSaveDto.getfLastStr(), "后缀不能为空！");
        Assert.isBlank(productNumberSaveDto.getStartNo(), "开始序号不能为空！");
        Assert.isEmpty(productNumberSaveDto.getLocation(), "序号放置位置不能为空！");
        Assert.isEmpty(productNumberSaveDto.getMocoRders().getfQty(), "产品数量不能为空！");
        Assert.isTrue(productNumberSaveDto.getMocoRders().getfQty().compareTo(BigDecimal.ZERO) <= 0, "产品数量必须大于0");
        List<ProductNumber> save = productNumberService.savePn(productNumberSaveDto);
        if (save.isEmpty()) return R.failed(HttpStatus.BAD_REQUEST.value(), "操作失败");
        return R.success(save.stream().map(data -> BeanUtil.copyProperties(data, ProductNumberVo.class)).collect(Collectors.toList()));
    }

    @PostMapping("/gms")
    @ApiOperation("获取最大的序号")
    public R<String> getMaxSeqNo(@RequestBody ProductNumberDto productNumberDto) throws Exception {
        Assert.isEmpty(productNumberDto.getfFront(), "前缀不能为空！");
        Assert.isEmpty(productNumberDto.getfLastStr(), "后缀不能为空！");
        Assert.isEmpty(productNumberDto.getLocation(), "编码位置不能为空！");
        String seqNo = productNumberService.getMaxSeqNo(productNumberDto);
        Assert.isEmpty(seqNo, "暂无此制令号条码！");
        return R.success(addStrings(seqNo, "1"));
    }

    public static String addStrings(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        int maxLen = Math.max(len1, len2);
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < maxLen; i++) {
            int digit1 = i < len1 ? num1.charAt(len1 - 1 - i) - '0' : 0;
            int digit2 = i < len2 ? num2.charAt(len2 - 1 - i) - '0' : 0;
            int sum = digit1 + digit2 + carry;
            carry = sum / 10;
            sb.append(sum % 10);
        }
        if (carry > 0) {
            sb.append(carry);
        }
        return sb.reverse().toString();
    }

    @PostMapping("/bd")
    @ApiOperation("批量删除产品序号")
    @ApiImplicitParam(name = "fids", value = "产品序号字符串集")
    public R<Integer> batchDel(@RequestBody List<String> fids) {
        if (fids.isEmpty())return R.success(0);
        return R.success(productNumberService.batchDel(fids));
    }

    @PostMapping("/upn")
    @ApiOperation("修改产品序号信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "fId", value = "内码"), @ApiImplicitParam(name = "bar", value = "产品条码")})
    public R<Integer> updateProductNumber(String fId, String bar) throws Exception {
        if (Helper.isEmpty(fId)) throw new BusinessException(HttpStatus.BAD_REQUEST, "fId不能为空！");
        if (Helper.isEmpty(bar)) throw new BusinessException(HttpStatus.BAD_REQUEST, "产品条码内容不能为空！");
        int number = productNumberService.updateProductNumber(fId, bar);
        if (number <= 0) throw new BusinessException("不是草稿状态不能修改！");
        return R.success(number);
    }

}
